<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;
class ProjectController extends AdminBaseController
{
    //查看资料
    public function index()
    {
        return $this->fetch('project_list');
    }

    //项目列表
    public function projectList(Request $resquest)
    {
        $parameter = $resquest->param();
        $id = $parameter['post_id'];
        $category_id = Db::name('portal_category_post')->where(['post_id'=>$id,'status'=>1])->value('category_id');
        //判断是否是这个项目的人
        $user_id = session('ADMIN_ID');
        $user_role = Db::name('user')->where('id',$user_id)->field('branch_id,authority')->find();
        
        if($user_role['authority'] == 'admin' || $user_role['authority'] == '管理员' || $user_role['authority'] == '总经理'){
        }else{
            if($user_role['branch_id'] != $category_id){$this->error(lang('暂无权限访问此项目!!!'));}
        }   
        //项目名
        $category = Db::name('portal_category')->where('id',$category_id)->field('id,name,parent_id')->find();
        //查出板块文章
        $plate_name = Db::name('portal_category')->where('id',$category['parent_id'])->value('name');
		$post_id_plate = Db::name('portal_category_post')->where(['category_id'=>$category['parent_id'],'status'=>1])->value('post_id');
        $plate_whe = [
            'post_status' => 1,
            'is_top' => 1,
			'id' => $post_id_plate,
			'delete_time' => 0,
            'post_title' => $plate_name,
        ];
        $plate = Db::name('portal_post')->where($plate_whe)->field('id,thumbnail,post_excerpt,post_title')->find();
	
        //查出项目下所有的文章,不包括标题是项目名的
        $post = Db::name('portal_category_post')->where(['category_id'=>$category_id,'status'=>1])->field('post_id')->select();
        $post_res = array();
        foreach($post as $k=>$v){
            $post_res[] = $v['post_id'];
        }
        $project_res = Db::name('portal_post')->where('post_status',1)->whereNotIn('post_title',$category['name'])->whereIn('id',$post_res)->field('id,thumbnail,post_excerpt,post_title,published_time')->paginate(5,false,['query'=>request()->param()]);
        $this->assign(['project_res' => $project_res,'plate'=>$plate,'category_name'=>$category['name']]);
        $this->assign(['project_list' => $project_res->render()]);
        return $this->fetch('project:list');
    }

    //项目详情
    public function projectDetails(Request $resquest)
    {     
        $parameter = $resquest->param();
        $id = $parameter['post_id'];
        $category_id = Db::name('portal_category_post')->where(['post_id'=>$id,'status'=>1])->value('category_id');
        //项目名
        $category = Db::name('portal_category')->where('id',$category_id)->field('id,name,parent_id')->find();
        //查出板块文章
        $plate_name = Db::name('portal_category')->where('id',$category['parent_id'])->value('name');
        $plate_whe = [
            'post_status' => 1,
            'is_top' => 1,
			'delete_time' => 0,
            'post_title' => $plate_name,
        ];
        $plate = Db::name('portal_post')->where($plate_whe)->field('id,thumbnail,post_excerpt,post_title')->find();
        //对应详情文章
        $post_res = Db::name('portal_post')->where('id',$id)->field('id,user_id,thumbnail,post_excerpt,post_content,post_title,post_hits,published_time')->find();
        $post_res['published_time'] = date('Y-m-d H:i:s', $post_res['published_time']);
        //发布人
        $user_name = Db::name('user')->where('id',$post_res['user_id'])->value('user_login');
        //上一条id
        $previous = Db::name('portal_category_post')->where(['category_id'=>$category_id,'status'=>1])->where('post_id','<',$id)->field('post_id')->find();
        if(empty($previous)){$previous_id = '';}
        $previous_id = $previous['post_id'];
        //下一条id
        $next = Db::name('portal_category_post')->where(['category_id'=>$category_id,'status'=>1])->where('post_id','>',$id)->field('post_id')->find();
        if(empty($next)){$next_id = '';}
        $next_id = $next['post_id'];
        $this->assign(['plate' => $plate,'post_res' => $post_res,'user_name'=> $user_name,'previous_id'=>$previous_id,'next_id'=>$next_id,'category_name'=>$category['name']]);
        return $this->fetch('project_details');
    }

}
